//
//  JXSplashController.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/18.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//
#import <AdSupport/AdSupport.h>
#import "JXSplashController.h"
#import "JXTabBarController.h"
#import "HomeDataTool.h"
#import "UserDefaultsHelper.h"
#import "MJExtension.h"
#import "JXAppInfo.h"
#import "JXNavigationController.h"
#import "JXPrivacyPwdController.h"
#import "JXGuideController.h"
#import "HomeInfoDao.h"
@interface JXSplashController ()<PrivacyPwdDelegate>
{
    UInt64 startTime;
}
//@property(nonatomic,weak)UIImageView *bg;
@end

@implementation JXSplashController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 1.背景图片
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    UIImageView *bg = [[UIImageView alloc] init];
    JXLog(@"height=%f",height);
    
    if(height==480){// 4s 4
        bg.image = [UIImage imageNamed:@"splash_image"];
    }else if(height==568){// 5s 5
        bg.image = [UIImage imageNamed:@"splash_image_568"];
    }else if(height==667){// 6
        bg.image = [UIImage imageNamed:@"splash_image_667"];
    }else if(height==736){// 6 plus
        bg.image = [UIImage imageNamed:@"splash_image_736"];
    }
    bg.frame = self.view.bounds;
    [self.view addSubview:bg];
    
    startTime = [[NSDate date] timeIntervalSince1970]*1000;
    
    NSString *appVersion = [UserDefaultsHelper stringForKey:KEY_APP_VERSION];
    if ([Utils isBlankString:appVersion]) {
        NSString *adId = [[[ASIdentifierManager sharedManager]
                           advertisingIdentifier] UUIDString];
        [HomeDataTool setADIDFA:adId];
    }
    
    [HomeDataTool appInfo:^(JXAppInfo *result) {
        JXLog(@"%@",result);
        //判断是首页标签是否清除缓存
        if(result.isCloseOtherLabel == 1){//清楚缓存
            HomeInfoDao *dao = [[HomeInfoDao alloc]init];
            [dao deleteAllHistory];
        }
        [UserDefaultsHelper setBool:result.bbsController forKey:KEY_SHOW_FORUM];
        // 归档
        [NSKeyedArchiver archiveRootObject:result toFile:JXAppInfoFilePath];
        
        //存入文件里面去
        [self startApp];
        
        //解档
//        [NSKeyedUnarchiver unarchiveObjectWithFile:JXAppInfoFilePath];
        
    } failure:^(NSError *error) {
        [self toMainTabController];
    }];
}


-(void)startApp{
    if (![Utils isBlankString:[UserDefaultsHelper stringForKey:KEY_HAVE_PASSWORD]]) {
        //需要进行到密码页面输入密码才能进入
        JXPrivacyPwdController *ctrl = [[JXPrivacyPwdController alloc]initWithFromType:FROM_TYPE_SPLASH];
        ctrl.delegate = self;
        JXNavigationController *nav = [[JXNavigationController alloc]initWithRootViewController:ctrl];
        [self presentViewController:nav animated:YES completion:nil];
    }else{
        //直接进入
        [self toMainTabController];
    }
}

-(void)toMainTabController{
    //判断是否需要进行guide页面
    
    NSString *appVersion = [UserDefaultsHelper stringForKey:KEY_APP_VERSION];
    if ([Utils isBlankString:appVersion] || ![appVersion isEqualToString:[Utils getAppVersion]]) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.rootViewController = [[JXGuideController alloc]init];
        [window makeKeyAndVisible];
    }else{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.rootViewController = [[JXTabBarController alloc]init];
        [window makeKeyAndVisible];
    }
}

-(void)endSetPwd{
    [self toMainTabController];
}




@end
